// Ionic_SDK
// 
// 保佑没Bug 
//
// Created by AS on 2019/5/12. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "CollectionImageCell.h"

@interface CollectionImageCell()
/**  */
@property (nonatomic,strong) UIImageView *imageView;
/**  */
@property (nonatomic,strong) UIButton *deleteBtn;
/**  */
@property (nonatomic,strong) id image;
/**  */
@property (nonatomic,assign) NSInteger index;
@end

@implementation CollectionImageCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}


-(void)initSubView{
    
    self.imageView = [[UIImageView alloc]init];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.layer.masksToBounds = YES;
    [self addSubview:self.imageView];
    
    self.deleteBtn = [UIButton new];
    [self.deleteBtn setImage:[UIImage imageNamed:@"deleted"] forState:UIControlStateNormal];
    self.deleteBtn.backgroundColor = [UIColor whiteColor];
    [self.deleteBtn setLayerWithRadius:12.5 color:ClearColor width:0];
    [self.deleteBtn addTarget:self action:@selector(deletedAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.deleteBtn];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self);
        make.width.mas_offset(25);
        make.height.mas_offset(25);
    }];
}


-(void)setImage:(id)image withIndex:(NSInteger)index{
    _image = image;
    _index = index;
    if ([image isKindOfClass:[NSString class]]) {
        self.imageView.image = [UIImage imageNamed:image];
        self.deleteBtn.hidden = YES;
    }else if ([image isKindOfClass:[UIImage class]]){
        self.imageView.image = image;
        self.deleteBtn.hidden = NO;
    }
}

-(void)deletedAction{
    if (self.deletedBlock) {
        self.deletedBlock(self.index);
    }
}

@end
